package com.genexususa.eo.forddirect.connection;


import com.genexususa.eo.forddirect.data.Properties;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

import javax.ws.rs.client.*;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.io.IOException;
import java.util.HashMap;

public class CampaignManager {

    private String URL = "https://www.das-qa.forddirect.com/api/1.0/";

    private static String campaignResource = "campaign";
    private static String slashSeparator = "/";

    private final static Logger log = Logger.getLogger(CampaignManager.class);

    public CampaignManager(){
        PropertyConfigurator.configure("src/main/resources/log4j.properties");
    }


    public void SetURL(String url){
        this.URL = url;
    }

    public void CreateCampaign(String dealerCode, String providerId, String campaignId, String subscriptionId, String channel, String fundingSource,
                               String name, int budget, String status){
        log.info("Create Campaign called with parameters dealerCode: " + dealerCode + ", providerId: " + providerId + ", campaignId: " + campaignId
                 + ", subscriptionId: " + subscriptionId + ", channel: " + channel + ", fundingSource: " + fundingSource + ", name: " + name
                 + ", budget: " + budget + ", status: " + status);

        try{
            HashMap<String, String> props = Properties.getFordDirectProperties();


            HttpAuthenticationFeature feature = HttpAuthenticationFeature.basic(props.get("userName"), props.get("password"));


            Client client = ClientBuilder.newClient();

            client.register(feature);

            WebTarget webTarget = client.target(this.URL);

            WebTarget resourceWebTarget = webTarget.path(campaignResource + slashSeparator + providerId + slashSeparator + campaignId);


            JSONObject input = new JSONObject();
            input.put("dealer_code", dealerCode);
            input.put("channel", channel);
            input.put("funding_source", fundingSource);
            input.put("budget", new Integer(budget));
            input.put("subscription_id", subscriptionId);
            input.put("name", name);
            input.put("status", status);


            Invocation invocation = resourceWebTarget.request().header("Content-Type", "application/json").header("charset", "UTF-8")
                                                     .buildPut(Entity.entity(input.toJSONString(), MediaType.APPLICATION_JSON_TYPE));

            Response response = invocation.invoke();

            JSONParser parser = new JSONParser();

            JSONObject jsonObj = (JSONObject) parser.parse(response.readEntity(String.class));

            log.info("Resource called: " + campaignResource + slashSeparator + providerId + slashSeparator + campaignId);
            log.info("Response: " + jsonObj.toJSONString());
            log.info("Response Status Code:" + response.getStatus());

            response.close();

        }catch (org.json.simple.parser.ParseException e) {
            log.error(e.getMessage());
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        } catch (IOException e) {
            log.error(e.getMessage());
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
    }

    public void GetCampaign(String providerId, String campaignId){

        log.info("GetCampaign Called with parameters providerId: " + providerId + " and campaignId: " +campaignId);

        try{
            HashMap<String, String> props = Properties.getFordDirectProperties();

            HttpAuthenticationFeature feature = HttpAuthenticationFeature.basic(props.get("userName"), props.get("password"));


            Client client = ClientBuilder.newClient();

            client.register(feature);

            WebTarget webTarget = client.target(this.URL);

            WebTarget resourceWebTarget = webTarget.path(campaignResource + slashSeparator + providerId + slashSeparator + campaignId);

            Response response = resourceWebTarget.request().get();

            JSONParser parser = new JSONParser();

            JSONObject jsonObj = (JSONObject) parser.parse(response.readEntity(String.class));

            log.info("Resource called: " + campaignResource + slashSeparator + providerId + slashSeparator + campaignId);
            log.info("Response: " + jsonObj.toJSONString());
            log.info("Response Status Code:" + response.getStatus());

            response.close();


        } catch (ParseException e) {
                log.error(e.getMessage());
                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        } catch (IOException e) {
            log.error(e.getMessage());
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
    }
}
